package org.wlgzs.shop.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.wlgzs.shop.bean.*;
import org.wlgzs.shop.dao.*;
import org.wlgzs.shop.service.IGoodsDateService;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

@Service(value = "GoodsDateService")
public class GoodsDateServiceImpl implements IGoodsDateService {
    @Autowired
    GoodsDataMapper goodsDataMapper;

    @Autowired
    GoodsAllKindMapper goodsAllKind;

    @Autowired
    GoodsAllKindTwoMapper goodsAllKindTwo;

    @Autowired
    StoreMapper storeMapper;

    @Autowired
    CarDataMapper carDataMapper;

    private static final Logger logger = LoggerFactory.getLogger(GoodsDateServiceImpl.class);

    /**
     *根据商品信息模糊查询
     * @param goodsName
     * @return List<GoodsData>
     */
    @Override
    public List<GoodsData> goodsNameSelectGoodsDate(String goodsName) {
        GoodsDataExample goodsDataExample=new GoodsDataExample();
        GoodsDataExample.Criteria criteria = goodsDataExample.createCriteria();
        criteria.andGoodsNameLike("%"+goodsName+"%");
        List<GoodsData> goodsDataList = goodsDataMapper.selectByExample(goodsDataExample);
        return goodsDataList;
    }


    //添加商品
    @Override
    public Msg addGood(GoodsData goodsData,String twoKind) {
        goodsData.setGoodsKind(twoKind);
        goodsDataMapper.insert(goodsData);
        Msg msg=new Msg();
        msg.add("goods","商品添加成功！");
        System.out.println(msg);
        return msg;
    }


    //修改商品价格
    @Override
    public Msg setGoodPerice(GoodsData data) {
        goodsDataMapper.updateByPrimaryKeySelective(data);
        return new Msg().add("setPerice","修改商品价格成功！");
    }

//根据goodsId删除商品
    @Override
    public Msg deleteGoods(int goodsId) {
        Msg msg=new Msg();
       int number=goodsDataMapper.deleteByPrimaryKey(goodsId);
       if(number>0) {
           msg.add("deleteGoods", "商品删除完毕！");
           msg.setCode(100);
       }
       else{
           msg.add("deleteGoods", "商品删除是失败！");
           msg.setCode(200);
       }
        return msg;
    }

    //根据一级分类的名称获取表单对象
    public GoodsAllKind selectGoodsKindOne(String goodsKind){
        GoodsAllKindExample goodsAllKindExample=new GoodsAllKindExample();
        GoodsAllKindExample.Criteria criteria=goodsAllKindExample.createCriteria();
        criteria.andGoodsKindoneEqualTo(goodsKind);
        List<GoodsAllKind> list=goodsAllKind.selectByExample(goodsAllKindExample);
        return list.get(0);
    }






    //根据商品名称获取所有商品信息
    public List<GoodsData> selectAllGoods(String goodsName){
        GoodsDataExample goodsDataExample=new GoodsDataExample();
        GoodsDataExample.Criteria criteria=goodsDataExample.createCriteria();
        criteria.andGoodsNameEqualTo(goodsName);
        List<GoodsData> List=goodsDataMapper.selectByExample(goodsDataExample);
        return  List;
    }

    //根据商品的名称获取对象
    public GoodsData selectGoods(String goodsName,int storeId){
        List<GoodsData> List=selectAllGoods(goodsName);
        for(GoodsData goodsData:List){
            if(goodsData.getStoreId()==storeId){
                return goodsData;
            }
        }
        return  null;
    }

    @Cacheable(value = "allGoods")
    @Override
    public List<GoodsData> allGoods(){
        return goodsDataMapper.selectByExample(null);
    }


    @Override
    public GoodsData selectGoods(int id) {
        GoodsData goodsData=goodsDataMapper.selectByPrimaryKey(id);
        if(goodsData==null) {
            logger.info("未含有该商品！");
            return null;
        }
        else {
            return goodsData;
        }
    }

    @Cacheable(value = "selectTwoKind")
    @Override
    public List<GoodsData> selectDate(String twoKind) {
        GoodsDataExample goodsDataExample=new GoodsDataExample();
        GoodsDataExample.Criteria criteria=goodsDataExample.createCriteria();
        criteria.andGoodsKindEqualTo(twoKind);
        return goodsDataMapper.selectByExample(goodsDataExample);
    }

    //获取goodsId和图片路径
    @Override
    public List<ShowDate> selectGoodsShow(List<GoodsData> datelist, int number){
        List<ShowDate> list= new ArrayList<ShowDate>();
        String[] date;
        int a=0;
        if(number>datelist.size()){
            number=datelist.size();
        }
        for(GoodsData data:datelist){
            a++;
            String[] photoDate=data.getPhotoUrl().split(",");
            ShowDate showDate=new ShowDate();
            showDate.setPrice(data.getGoodsPrice());
            showDate.setPhotoName(photoDate[0]);
            showDate.setId(data.getId());
            showDate.setName(data.getGoodsName());
            list.add(showDate);
            if(a>=number){
                break;
            }
        }
        return list;
    }

    //根据店铺查询商品
    @Override
    @Cacheable(value = "storeGoods")
    public List<ShowDate> selectByStore(int storeId){
        GoodsDataExample goodsDataExample=new GoodsDataExample();
        GoodsDataExample.Criteria criteria=goodsDataExample.createCriteria();
        criteria.andStoreIdEqualTo(storeId);
        List<GoodsData> list=goodsDataMapper.selectByExample(goodsDataExample);
       return selectGoodsShow(list ,list.size());
    }

    @Override
    public List<GoodsData> selectGoodsByStore(int storeId) {
        GoodsDataExample goodsDataExample=new GoodsDataExample();
        GoodsDataExample.Criteria criteria=goodsDataExample.createCriteria();
        criteria.andStoreIdEqualTo(storeId);
        List list=goodsDataMapper.selectByExample(goodsDataExample);
        System.out.println(list.size()+"===========");
        return list;
    }


    //根据商店的id二级分类id同时查询
    @Override
    public List<ShowDate> selectByStoreTwoKind(int oneKindId,String storeId,String twoKindId) {
            GoodsDataExample goodsDataExample=new GoodsDataExample();
            List<GoodsData> list;
            GoodsDataExample.Criteria criteria=goodsDataExample.createCriteria();
            logger.info(storeId);
            logger.info(twoKindId);
            criteria.andGoodsStyleEqualTo(oneKindId+"");
            if(storeId!=null){
                int stored=Integer.parseInt(storeId);
                criteria.andStoreIdEqualTo(stored);
            }
            if(twoKindId!=null){
                criteria.andGoodsKindEqualTo(twoKindId);
            }
            if(storeId==null&&twoKindId==null){
                goodsDataExample=null;
             }

            list=goodsDataMapper.selectByExample(goodsDataExample);
        return selectGoodsShow(list,list.size());
    }

    //排序查询商品
    @Cacheable(value = "descGoods")
    public List<GoodsData> descGoods(){
        GoodsDataExample goodsDataExample=new GoodsDataExample();
        goodsDataExample.setOrderByClause("buy_number");
        List<GoodsData> list= goodsDataMapper.selectByExample(goodsDataExample);
        System.out.println(list);
        return list;
    }

    //查询热销产品
    @Override
    public List<ShowDate> selectHotGoods() {
        return selectGoodsShow(descGoods(),16);
    }

    //模糊查询商品
    @Override
    public List<GoodsData> selectGoods(String date) {
            GoodsDataExample goodsDataExample=new GoodsDataExample();
            GoodsDataExample.Criteria criteria=goodsDataExample.createCriteria();
            criteria.andGoodsNameLike("%"+date+"%");
            List<GoodsData> list=goodsDataMapper.selectByExample(goodsDataExample);
            return list;
    }

    //模糊查询，返回路径
    @Override
    public String selectTrueGoods(String date) {
        StringBuffer buffer=new StringBuffer();
        GoodsAllKindTwoExample goodsAllKindTwoExample=new GoodsAllKindTwoExample();
        GoodsAllKindTwoExample.Criteria criteria=goodsAllKindTwoExample.createCriteria();
        criteria.andGoodsKindTwoLike("%"+date+"%");
        List<GoodsAllKindTwo> goodsDateKind=goodsAllKindTwo.selectByExample(goodsAllKindTwoExample);
        if(goodsDateKind.size()!=0){
            buffer.append("redirect:/catch?twoKindId=");
            buffer.append(goodsDateKind.get(0).getId());
            return buffer.toString();
        }
        else if (selectGoods(date).size()!=0) {
            buffer.append("redirect:/catch?twoKindId=");
            buffer.append(selectGoods(date).get(0).getGoodsKind());
              return buffer.toString();
            }
        return "/";
        }

    @Override
    public List<GoodsData> selectGoodsByUserID(int userId) {
        GoodsDataExample goodsDataExample=new GoodsDataExample();
        GoodsDataExample.Criteria criteria=goodsDataExample.createCriteria();
        return null;
    }

    @Override
    public void selectGoodsShow(List<GoodsData> goodsData) {

    }

    @Override
    public void delectCar(int id,int userId) {
         CarDataExample carDataExample=new CarDataExample();
         CarDataExample.Criteria criteria=carDataExample.createCriteria();
         criteria.andPaymentEqualTo(id+"");
         criteria.andStoreNameEqualTo(userId+"");
         carDataMapper.deleteByExample(carDataExample);

    }

    @Override
    public List<GoodsData> selectGoodsBystoreID(int storeId) {
        GoodsDataExample goodsDataExample=new GoodsDataExample();
        GoodsDataExample.Criteria criteria=goodsDataExample.createCriteria();
        criteria.andStoreIdEqualTo(storeId);
        return goodsDataMapper.selectByExample(goodsDataExample);
    }


    //根据店铺查询商品的分类
    @Override
    public List<String> selectKindByStore(int storeId){
        GoodsDataExample goodsDataExample=new GoodsDataExample();
        goodsDataExample=new GoodsDataExample();
        GoodsDataExample.Criteria criteria=goodsDataExample.createCriteria();
        criteria.andStoreIdEqualTo(storeId);
        List<String> list=new ArrayList<String>();
        HashSet<String> set=new HashSet<String>();
       for( GoodsData data:goodsDataMapper.selectByExample(goodsDataExample)){
            set.add(data.getGoodsKind());
       }
        for (String date:set){
            list.add(date);
        }
            return list;
    }

}